package com.iot.ic.modules.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iot.ic.modules.dao.UserDao;
import com.iot.ic.modules.model.Amounts;
import com.iot.ic.modules.model.User;
import com.iot.ic.modules.service.UserService;

@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Override
    public User findByMobile(String mobile,int userType) throws Exception {
        return userDao.findByMobile(mobile,userType);
    }

    @Override
    public User findByNameOrMobile(String name,int userType) throws Exception {
        return userDao.findByNameOrMobile(name,userType);
    }

    @Override
    public User findByName(String userName,int userType) throws Exception {
        return userDao.findByName(userName,userType);
    }

    @Override
    public User saveOrUpdateObject(User user) throws Exception {
        User u= (User) userDao.addOrUpdateObject(user);
        if(u!=null){
            Amounts amounts=new Amounts();
            amounts.setState(0);
            amounts.setUserId(u.getId());
            userDao.addOrUpdateObject(amounts);
        }
        return u;
    }

    @Override
    public User updateUser(User user) throws Exception {
        if(userDao.updateUser(user)){
            return userDao.findById(user.getId(),user.getUserType());
        }else{
            return null;
        }
    }

    @Override
    public User findById(Integer id,int userType) throws Exception {
        return userDao.findById(id, userType);
    }

    @Override
    public void deleteUserById(User user) throws Exception {
        userDao. deleObject(user);
    }

    @Override
    public boolean updateUserRegId(Integer userId, String mRegId) throws Exception {
        return  userDao. updateUserRegId(userId,mRegId);
    }

	@Override
	public List<User> findUser(Integer id, String name, String hospital, int i) throws Exception {
		return userDao. findUser( id,  name,  hospital,  i);
	}


}
